﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class AudioPro : MonoBehaviour {

	/// <summary>
	/// 音效种类
	/// </summary>
	public enum Type{
		None,
		BGM,
		SE

	}

	#region 音效相关数据
	private AudioSource myAudioSource;

	[SerializeField]
	[Tooltip("音效种类")]
	private Type type=Type.SE; 
	/// <summary>
	/// 只读属性
	/// </summary>

	public  Type TheType{
		get{
			return type;
		}
	}
    #endregion

    private void Awake()
    {
        myAudioSource = GetComponent<AudioSource>();
        myAudioSource.playOnAwake = false;
    }

    void Start()
	{
		switch(TheType){
			case Type.None: return;
			case Type.BGM :{Play();myAudioSource.loop=true;break;}
			case Type.SE  :break;
        }
        EventManager.Instance.AddListener(EVENT_TYPE.SETTING, OnMyEvent);
    }

	public void Play()
	{
		switch(TheType){
			case Type.None: return;
			case Type.BGM :myAudioSource.volume=SettingManager.Instance.BackVolume;break;
			case Type.SE  :myAudioSource.volume=SettingManager.Instance.MainVolume;break;
		}
		myAudioSource.Play();
	}

	public void ChangeAudio(AudioClip sc)
	{
		myAudioSource.clip=sc;
        myAudioSource.Play();
	}

    void OnMyEvent(EVENT_TYPE Event_Type,
        Component Sender, object Param = null)
    {
        switch (TheType)
        {
            case Type.None: return;
            case Type.BGM: myAudioSource.volume = SettingManager.Instance.BackVolume; break;
            case Type.SE: myAudioSource.volume = SettingManager.Instance.MainVolume; break;
        }
    }

}
